﻿//********************************************************************************//
//创建日期：<创建日期，2016-01-11>
//创建作者：<郭登杰，guodengjie@shanghai3h.com>
//功能说明：用水信息_地下
//********************************************************************************//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;
using BLL;
using BLL.Access;
using System.Data;
using Platform.Http;
using System.Text;
using Web.Class;
using DevExpress.Web;

namespace Web.Pages.QuShui.XiTongJK
{
    public partial class YongShuiXX_DXS : BasePage
    {
        #region 属性


        /// <summary>
        /// 保存结果集
        /// </summary>
        private DataTable dtSource
        {
            get
            {
                return ViewState["dtSource"] as DataTable;
            }
            set
            {
                ViewState["dtSource"] = value;
            }
        }

        #endregion

        #region 变量

        /// <summary>
        /// 行政区对象
        /// </summary>
        YS_JX_XINGZHENGQ _YS_JX_XINGZHENGQ = new YS_JX_XINGZHENGQ();

        /// <summary>
        /// 行政区BLL
        /// </summary>
        YS_JX_XINGZHENGQBLL _YS_JX_XINGZHENGQBLL = new YS_JX_XINGZHENGQBLL();

        /// <summary>
        /// 操作日志BLL
        /// </summary>
        SYS_LOGBLL _SYS_LOGBLL = new SYS_LOGBLL();

        #endregion

        #region 事件

        /// <summary>
        /// 页面加载事件
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            //第一次页面加载
            if (!IsPostBack)
            {
                InitData();
            }
            else
            { 
                LoadData(true);
            }
            this.ScriptManager1.RegisterPostBackControl(this.btnExport);            
        }

        /// <summary>
        /// 绑定空表头
        /// </summary>
        protected override void Render(HtmlTextWriter sender)
        {
            base.Render(sender);
        }



        /// <summary>
        /// 响应前台回调
        /// </summary>
        protected void ASPxCallback1_Callback(object source, DevExpress.Web.CallbackEventArgs e)
        {

        }

        /// <summary>
        /// 客户端刷新list
        /// </summary>
        protected void gvList_CustomCallback(object sender, DevExpress.Web.ASPxGridViewCustomCallbackEventArgs e)
        {
            LoadData(false);
        }

        /// <summary>
        /// 导出到EXCEL
        /// </summary>
        protected void btnExport_Click(object sender, EventArgs e)
        {
            if (this.dtSource != null)
            {
                //获取选中的数据
                DataTable dt = PageTools.GetCheckedData(this.gvList, this.dtSource, "cid", "id");
                //筛选及重命名需要的列
                dt.Columns.Remove("ROWNUM");
                dt.Columns[0].ColumnName = "水量年月";
                dt.Columns[1].ColumnName = "用户号";
                dt.Columns[2].ColumnName = "用户名";
                dt.Columns[3].ColumnName = "水价";
                dt.Columns[4].ColumnName = "水表编号";
                dt.Columns[5].ColumnName = "抄表状态";
                dt.Columns[6].ColumnName = "本月水量";
                List<string> list = new List<string>();
                list.Add("70");
                list.Add("100");
                list.Add("250");
                list.Add("50");
                list.Add("70");
                list.Add("70");
                list.Add("80");
                PageTools.CreateExcelAccordWidths(dt,list, string.Format("{0}-{1}水量年月地下水用水信息.xls", this.txtYM1.Text, this.txtYM2.Text));
            }
        }

        #endregion

        #region 方法

        /// <summary>
        /// 初始化数据
        /// </summary>
        private void InitData()
        {

        }

        /// <summary>
        /// 数据加载
        /// </summary>
        private void LoadData(bool reget)
        {
            if (reget)
            {
                if (this.txtYM1.Text != "" && this.txtYM2.Text != "")
                {
                    this.dtSource = new SpHelper().P_QSXTJK_YONGSHUIXX(2, int.Parse(this.txtYM1.Text), int.Parse(this.txtYM2.Text)).Tables[0];
                }
                else
                {
                    this.dtSource = null;
                }
            }
            //记录选中项
            Dictionary<string, string> list = PageTools.GetCheckedValueList(this.gvList, "cid", "id");
            this.gvList.DataSource = this.dtSource;
            this.gvList.DataBind();
            //绑定后，给每一行的checkbox的clientname赋值
            for (int i = 0; i < this.gvList.VisibleRowCount; i++)
            {
                ASPxCheckBox cb = (ASPxCheckBox)gvList.FindRowCellTemplateControl(i, (GridViewDataColumn)gvList.Columns[0], "cid");
                cb.ClientInstanceName = "checked" + i;
            }
            //绑定后默认全选
            PageTools.SetCheckedAll(this.gvList,"cid",true);
            //重新选中项
            PageTools.SetCheckedValueList(this.gvList, "cid", "id", list);
        }

        #endregion

        protected void gvList_CustomCallback1(object sender, DevExpress.Web.ASPxGridViewCustomCallbackEventArgs e)
        {
            //LoadData(false);
        }
    }
}